<?php

// Start Session
session_start();
require_once('Authorizer.php');
require_once('DBConfigQuery.php');
require_once('GenFunctions.php');
require_once('../config.inc.php');
require_once('../Validation.php');


if (isset($_POST['EditEditEvent'])) {
    session_write_close();
    if (isset($_SESSION['FORMS']['FormEditEvent']))
    {
        $eventIdentification = $_SESSION['FORMS']['FormEditEvent']['EventId'];
        unset ($_SESSION['FORMS']['FormEditEvent']);
        header('Location: AdminEventsEditEvent.php?eventid=' . $eventIdentification );
    }
    else
    {
        header('Location: AdminEventEdit.php');
    }
}

// TODO VALIDATE THE INFO BEFORE THIS SCRIPT...
//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = FALSE;

// TODO VALIDATE THE INFO BASED ON VALIDATION.PHP/Validation Functions
//Sanitize the POST values
$etitle = clean($_POST['etitle'], $dbc);
$espkrname = clean($_POST['espkrname'], $dbc);
$espkrinfo = clean($_POST['espkrinfo'], $dbc);

$edate = clean($_POST['edate'], $dbc);
$etime = clean($_POST['etime'], $dbc);

$evenuename = clean($_POST['evenuename'], $dbc);
$evenueinfo = clean($_POST['evenueinfo'], $dbc);

$etype = clean($_POST['etype'], $dbc);
$estatus = clean($_POST['estatus'], $dbc);

$efeemem = clean($_POST['efeemem'], $dbc);
$efeeguest = clean($_POST['efeeguest'], $dbc);

$edescription = rtrim(ltrim(clean($_POST['edescription'], $dbc)));

$eventId = $_SESSION['FORMS']['FormEditEvent']['EventId'];

//If Input Errors, Redirect EventCreatePage
if ($errflag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    $_SESSION['FORMS']['FormCreateEvent'] = $_POST;
    session_write_close();
    header('Location: AdminEventCreate.php');
    exit();
}


if (strcasecmp($_SESSION['FORMS']['FormEditEvent']['Status'], 'U') == 0 || strcasecmp($_SESSION['FORMS']['FormEditEvent']['Status'], 'B') == 0 )
{
    // The event status WAS either Upcoming or Blocked
    if (strcasecmp($estatus, 'U') == 0 || strcasecmp($estatus, 'B') == 0 )
    {
        // The status is unchanged or changed from Upcoming to Blocked or from Blocked to Upcoming
        break;
    }
    else
    {
        // The status has changed or changed from Upcoming/Blocked to Normal/Cancelled
        // TODO: There might be some cleaning/update to set up an event as the one to be upcoming
        //       Depends on Requirements (We might leave it blank
    }
}
elseif (strcasecmp($estatus, 'U') == 0 || strcasecmp($estatus, 'B') == 0 )
{
    // The event status WAS not Upcoming nor Blocked and it IS going to be set as Upcoming or Blocked
    // 
    // Clear other events since this one is going to be the Upcoming/Blocked
    // which could only be one in the DB
    mysqli_query($dbc,$U_None_None_EventStatus) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
}


$q = sprintf($U_ALL_Eventid, changeFormDateToSQLDate($edate), $etitle, $espkrname, $espkrinfo, $etime,
                            $evenuename, $evenueinfo, $etype, $estatus,
                            $efeemem, $efeeguest, $edescription, $eventId);

$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

//Check whether the query was successful or not
if ($r) {
    mysqli_close($dbc);
    session_write_close();
    header("Location: AdminEventEditSuccess.php");
    exit();
} else {
    // Free DB
    mysqli_close($dbc);
    die("Query failed");
}
?>